package com.creatingrevolutions.vbukit.lib.zerologin;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.creatingrevolutions.vbukit.lib.InternetResponseModel;
import com.creatingrevolutions.vbukit.lib.InternetUtils;
import com.creatingrevolutions.vbukit.lib.handler.BaseVbukitHandler;

import android.content.Context;
import android.util.Log;

public class ZeroLoginManager
{	
	final static String SERVER = "http://54.235.209.16";
	
	public static void createZeroLoginBucket(final BaseVbukitHandler handler)
	{
		Thread t = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				requestTemporaryBucketCreation(new BaseVbukitHandler()
				{
					@Override
					public void updateInfo(boolean result, 
							Object oldValue, Object newValue)
					{
						if ( result ) 
						{
							try 
							{
								String strJson = (String)newValue;
								JSONObject json = new JSONObject(strJson);
								
								String bucketIdPermanent = 
										json.getString("bucketIdPermanent");
								
								String bucketIdDynamic = 
										json.getString("bucketIdDynamic");
								
								JSONObject jsonMessage = json.getJSONObject("message");
								
								String message = 
										jsonMessage.getString("message");
								
								String messageType = 
										jsonMessage.getString("messageType");
								
								ZeroLoginModel model = new ZeroLoginModel();
								model.bucketIdPermanent = bucketIdPermanent;
								model.bucketIdDynamic = bucketIdDynamic;
								model.message = message;
								model.messageType = messageType;
								
								// return ok
								if ( handler != null ) {
									handler.updateInfo(true, json, model);
								}
							}
							catch ( Exception e ) 
							{
								e.printStackTrace();
								// return error
								if ( handler != null ) {
									handler.updateInfo(false, null, null);
								}
							}
						}
						else
						{
							// return error
							if ( handler != null ) {
								handler.updateInfo(false, null, null);
							}
						}
						
					}
				});
			}
		});
		t.start();
	}
	
	/**
	 * Don't make this method static because I'll use it
	 * only once in my app cycle.
	 * 
	 * @param handler - Return info using handler.
	 */
	public static void requestTemporaryBucketCreation(final BaseVbukitHandler handler)
	{
		Thread t = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				String strJson = requestTemporaryBucketCreationNoThread();
				
				if ( strJson != null )
				{
					if ( handler != null ) {
						handler.updateInfo(true, null, strJson);
					}
				}
				else
				{
					if ( handler != null ) {
						handler.updateInfo(false, null, null);
					}
				}
				
			}
		});
		t.start();
	}
	
	public static String requestTemporaryBucketCreationNoThread()
	{
		String url = SERVER + "/virtual-bucket/sender/" +
				"zerologin/createTemporaryBucket.html";
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		
		String strJson = null;
		
		try {
			strJson = InternetUtils.executeJsonPost(url, params);
			Log.i("requestTemporaryBucketCreationNoThread", 
					"strJson: " + strJson);
			return strJson;
		}
		catch ( Exception e ) 
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public static void getBucketFileInformation(final String bucketIdDynamic,
			final BaseVbukitHandler handler)
	{
		Thread t = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				String url = SERVER + "/virtual-bucket/sender/" +
						"zerologin/getBucketFileInfo.html";
				
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(
						"bucketIdDynamic", bucketIdDynamic));
				
				String strJson = null;
				
				try {
					strJson = InternetUtils.executeJsonPost(url, params);
					
					if ( handler != null ) {
						handler.updateInfo(true, null, strJson);
					}
				}
				catch ( Exception e ) 
				{
					e.printStackTrace();
					
					if ( handler != null ) {
						handler.updateInfo(false, null, null);
					}
				}
				
			}
		});
		t.start();
		
	}
	
	public static void downloadFile(final String bucketIdDynamic,
			final BaseVbukitHandler handler)
	{
		Thread t = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				InternetResponseModel model = 
						downloadFileNoThread(bucketIdDynamic);
				
				byte[] bucketFileInBytes = model.response;
				
				if ( bucketFileInBytes != null )
				{
					if ( handler != null ) {
						handler.updateInfo(true, null, bucketFileInBytes);
					}
				}
				else
				{
					if ( handler != null ) {
						handler.updateInfo(false, null, null);
					}
				}
			}
		});
		t.start();
		
	}
	
	public static InternetResponseModel downloadFileNoThread(String bucketIdDynamic)
	{
		String url = SERVER + "/virtual-bucket/sender/" +
				"zerologin/downloadFile.html";
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(
				"bucketIdDynamic", bucketIdDynamic));
		
		try {
			InternetResponseModel model = 
					InternetUtils.executePost(url, params);
			
			return model;
		}
		catch ( Exception e ) 
		{
			e.printStackTrace();
			return null;
		}
		
	}

	public static void fileReceivedConfirmation(final String bucketIdDynamic,
			final BaseVbukitHandler handler)
	{
		Thread t = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				String strJson = fileReceivedConfirmationNoThread(bucketIdDynamic);
				
				if ( strJson != null )
				{
					if ( handler != null ) {
						handler.updateInfo(true, null, strJson);
					}
				}
				else
				{
					if ( handler != null ) {
						handler.updateInfo(false, null, null);
					}
				}
			}
		});
		t.start();
		
	}
	
	public static String fileReceivedConfirmationNoThread(String bucketIdDynamic)
	{
		String url = SERVER + "/virtual-bucket/sender/" +
				"zerologin/fileDownloadedConfirmation.html";
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(
				"bucketIdDynamic", bucketIdDynamic));
		
		String strJson = null;
		
		try {
			strJson = InternetUtils.executeJsonPost(url, params);
			return strJson;
		}
		catch ( Exception e ) 
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static void removeCurrentTemporaryBucket(final String bucketIdDynamic,
			final BaseVbukitHandler handler)
	{
		Thread t = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				String url = SERVER + "/virtual-bucket/sender/" +
						"zerologin/removeTemporaryBucket.html";
				
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(
						"bucketIdDynamic", bucketIdDynamic));
				
				String strJson = null;
				
				try {
					strJson = InternetUtils.executeJsonPost(url, params);
					
					if ( handler != null ) {
						handler.updateInfo(true, null, strJson);
					}
				}
				catch ( Exception e ) 
				{
					e.printStackTrace();
					
					if ( handler != null ) {
						handler.updateInfo(false, null, null);
					}
				}
				
			}
		});
		t.start();
		
	}
	
}
